﻿using ULSLogParser;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Data;
using System.Windows.Forms;

namespace ProjectValidator
{
    
    
    /// <summary>
    ///This is a test class for ParserTest and is intended
    ///to contain all ParserTest Unit Tests
    ///</summary>
    [TestClass()]
    public class ParserTest
    {

        private const string COL_TIME_STAMP = "Timestamp";
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for OpenTabFile to pass empty params
        ///</summary>
        [TestMethod()]
        [ExpectedException(typeof(ArgumentException),
         "A FileName of null\\Empty was inappropriately allowed.")]

        public void OpenTabFileTestWithEmptyInputs()
        {
            string FileName = string.Empty; // TODO: Initialize to an appropriate value
            DataTable expected = new DataTable("Please supply file name"); // TODO: Initialize to an appropriate value
            DataTable actual;
            actual = Parser.OpenTabFile(FileName, FileName);
            Assert.AreEqual<string>(expected.TableName, actual.TableName,"The returned logs are not correct");
        }
        /// <summary>
        ///A test for OpenTabFile
        ///</summary>
        [TestMethod()]
        public void OpenTabFileTest()
        {
            //string FileName = @"C:\ML-Data\The Spot\MPLTTATIS104-20100815-1403.log"; //230 MB file -->275626 lines
            string FileName = @"C:\ML-Data\The Spot\MPLTTATIS102-20100810-0234.log"; //26 MB file -->28771
            int expectedRowCount = 28771;
            DataTable actual;
            actual = Parser.OpenTabFile(FileName, FileName);
            Assert.AreEqual<int>(expectedRowCount, actual.Rows.Count, "The returned logs are not correct");
            

        }

        public DataTable OpenTabFile()
        {
            //string FileName = @"C:\ML-Data\The Spot\ULS.log"; //180 KB file -->295
            //string FileName = @"C:\ML-Data\The Spot\MPLTTATIS104-20100815-1403.log"; //230 MB file -->275626 lines
            string FileName = @"C:\ML-Data\The Spot\MPLTTATIS102-20100810-0234.log"; //26 MB file -->28771
            
            DataTable actual;
            actual = Parser.OpenTabFile(FileName, FileName);
            return actual;

        }
        /// <summary>
        ///A test for ConnectLogs passing no parameters
        ///</summary>
        [TestMethod()]
        public void ConnectLogsTestWithNoParams()
        {
            DataTable Data = null; // TODO: Initialize to an appropriate value
            DataTable expected = null; // TODO: Initialize to an appropriate value
            DataTable actual = Data;
            Parser.ConnectLogs(Data);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for ConnectLogs 
        ///</summary>
        [TestMethod()]
        public void ConnectLogsTest()
        {
            DataTable Data = OpenTabFile();
            //Count rows with stars
            int StarsCount = 0;
            int Sets = 0;
            bool Set = false;
            //Get the number of stars
            foreach (DataRow item in Data.Rows)
            {
                if (item[COL_TIME_STAMP].ToString().Contains("*"))
                { StarsCount++; Set = true; }
                else if (Set)
                { Sets++; Set = false; }
            }
            DataTable actual;
            int expected = Data.Rows.Count - StarsCount;
            Parser.ConnectLogs(Data);
            actual = Data;
            Assert.AreEqual<int>(expected, actual.Rows.Count);
        }

        /// <summary>
        ///A test for GenerateSummary
        ///</summary>
        [TestMethod()]
        public void GenerateSummaryTest()
        {
            DataTable Data = null; // TODO: Initialize to an appropriate value
            Data = OpenTabFile();
            Parser.ConnectLogs(Data);
            Parser.IconMappingHandler iconMappingHandler = null; // TODO: Initialize to an appropriate value
            string RootNodeTitle = string.Empty; // TODO: Initialize to an appropriate value
            string RootNodeTooltip = string.Empty; // TODO: Initialize to an appropriate value
            
            TreeNode actual;
            actual = Parser.GenerateSummary(Data, iconMappingHandler, RootNodeTitle, RootNodeTooltip);
            int TotalCount = 0;
            foreach (TreeNode item in actual.Nodes)
            {
                int NodeChildrenCount = item.Nodes.Count;
                TotalCount += NodeChildrenCount + 1 - (NodeChildrenCount > 0 ? 1 : 0);
            }
            Assert.AreEqual(Data.Rows.Count, TotalCount);
        }

        /// <summary>
        ///A test for GenerateSummaryAI with 10% error param
        ///</summary>
        [TestMethod()]
        public void GenerateSummaryAITest10()
        {
            DataTable Data = null; // TODO: Initialize to an appropriate value
            Data = OpenTabFile();
            Parser.ConnectLogs(Data);
            Parser.IconMappingHandler iconMappingHandler = null; // TODO: Initialize to an appropriate value
            Decimal MatchErrorPrecentageAllowed = new Decimal(10); // TODO: Initialize to an appropriate value
            string RootNodeTitle = "LogFileName.Log"; // TODO: Initialize to an appropriate value
            string RootNodeTooltip = "C:\\LogFileName.Log"; // TODO: Initialize to an appropriate value
            TreeNode actual;
            Parser.GenerateSummary(Data, iconMappingHandler, RootNodeTitle, RootNodeTooltip);
            actual = Parser.GenerateSummaryAI(Data, iconMappingHandler, MatchErrorPrecentageAllowed, RootNodeTitle, RootNodeTooltip);
            int TotalCount = 0;
            foreach (TreeNode item in actual.Nodes)
            {
                int NodeChildrenCount = item.Nodes.Count;
                TotalCount += NodeChildrenCount + 1 - (NodeChildrenCount > 0 ? 1 : 0);
            }
            Assert.AreEqual(Data.Rows.Count, TotalCount);
        }

        /// <summary>
        ///A test for GenerateSummaryAI with 45% error param
        ///</summary>
        [TestMethod()]
        public void GenerateSummaryAITest45()
        {
            DataTable Data = null; // TODO: Initialize to an appropriate value
            Data = OpenTabFile();
            Parser.ConnectLogs(Data);
            Parser.IconMappingHandler iconMappingHandler = null; // TODO: Initialize to an appropriate value
            Decimal MatchErrorPrecentageAllowed = new Decimal(45); // TODO: Initialize to an appropriate value
            string RootNodeTitle = "LogFileName.Log"; // TODO: Initialize to an appropriate value
            string RootNodeTooltip = "C:\\LogFileName.Log"; // TODO: Initialize to an appropriate value
            TreeNode actual;
            Parser.GenerateSummary(Data, iconMappingHandler, RootNodeTitle, RootNodeTooltip);
            actual = Parser.GenerateSummaryAI(Data, iconMappingHandler, MatchErrorPrecentageAllowed, RootNodeTitle, RootNodeTooltip);
            int TotalCount = 0;
            foreach (TreeNode item in actual.Nodes)
            {
                int NodeChildrenCount = item.Nodes.Count;
                TotalCount += NodeChildrenCount + 1 - (NodeChildrenCount > 0 ? 1 : 0);
            }
            Assert.AreEqual(Data.Rows.Count, TotalCount);
        }
        /// <summary>
        ///A test for GenerateSummaryAI with 90% error param
        ///</summary>
        [TestMethod()]
        public void GenerateSummaryAITest90()
        {
            DataTable Data = null; // TODO: Initialize to an appropriate value
            Data = OpenTabFile();
            Parser.ConnectLogs(Data);
            Parser.IconMappingHandler iconMappingHandler = null; // TODO: Initialize to an appropriate value
            Decimal MatchErrorPrecentageAllowed = new Decimal(90); // TODO: Initialize to an appropriate value
            string RootNodeTitle = "LogFileName.Log"; // TODO: Initialize to an appropriate value
            string RootNodeTooltip = "C:\\LogFileName.Log"; // TODO: Initialize to an appropriate value
            TreeNode actual;
            Parser.GenerateSummary(Data, iconMappingHandler, RootNodeTitle, RootNodeTooltip);
            actual = Parser.GenerateSummaryAI(Data, iconMappingHandler, MatchErrorPrecentageAllowed, RootNodeTitle, RootNodeTooltip);
            int TotalCount = 0;
            foreach (TreeNode item in actual.Nodes)
            {
                int NodeChildrenCount = item.Nodes.Count;
                TotalCount += NodeChildrenCount + 1 - (NodeChildrenCount > 0 ? 1 : 0);
            }
            Assert.AreEqual(Data.Rows.Count, TotalCount);
        }

        /// <summary>
        ///A test for IncreamentSafeFileName Empty Params
        ///</summary>
        [TestMethod()]
        public void IncreamentSafeFileNameTestEmpty()
        {
            string SafeFileName = string.Empty;
            string actual;
            actual = Parser.IncreamentSafeFileName(SafeFileName);
            Assert.IsFalse(string.IsNullOrEmpty(actual));
            Assert.Inconclusive("Consider it success if this:'{0}' is 'GUID.log' format", actual);
        }


        /// <summary>
        ///A test for IncreamentSafeFileName file without extension
        ///</summary>
        [TestMethod()]
        public void IncreamentSafeFileNameTestFileWithoutExtension()
        {
            string SafeFileName = "File"; //
            string actual;
            actual = Parser.IncreamentSafeFileName(SafeFileName);
            Assert.IsFalse(string.IsNullOrEmpty(actual));
            Assert.Inconclusive("Consider it success if this:'{0}' is 'GUID.log' format", actual);
        }

        /// <summary>
        ///A test for IncreamentSafeFileName file without number
        ///</summary>
        [TestMethod()]
        public void IncreamentSafeFileNameTestFileWithoutNumber()
        {
            string SafeFileName = "File.log"; //
            string actual;
            string expected = "File1.log";
            actual = Parser.IncreamentSafeFileName(SafeFileName);
            Assert.AreEqual<string>(expected,actual);
            
        }

        /// <summary>
        ///A test for IncreamentSafeFileName file with a number
        ///</summary>
        [TestMethod()]
        public void IncreamentSafeFileNameTestFileWithNumber()
        {
            string SafeFileName = "File12349.log"; //
            string actual;
            string expected = "File12350.log";
            actual = Parser.IncreamentSafeFileName(SafeFileName);
            Assert.AreEqual<string>(expected, actual);

        }

        /// <summary>
        ///A test for IncreamentSafeFileName with file has double extensions
        ///</summary>
        [TestMethod()]
        public void IncreamentSafeFileNameTestFileWithNumberAndDoubleExtension()
        {
            string SafeFileName = "File12349.log.log"; //
            string actual;
            string expected = "File12349.log1.log";
            actual = Parser.IncreamentSafeFileName(SafeFileName);
            Assert.AreEqual<string>(expected, actual);

        }
    }
}
